import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.web.api.utils.DateUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;

/**
 * ClassName: Demo <br/>
 * Description: <br/>
 * date: 2020/8/25 15:06<br/>
 * @author fj<br />
 * @since JDK 1.8
 */
public class Demo {
    public static void main(String[] args) {
        String host = "10.104.203.166";
        int port = 8080;
        String URI = "https://uathmp.veevasfa.cn/public-api/v1/objects/account/records?filters[record_type]=hospital&limit=200&sort=-modified_on";

        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // 设置BasicAuth
        CredentialsProvider provider = new BasicCredentialsProvider();
        // Create the authentication scope
//        AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
        // Create credential pair，在此处填写用户名和密码
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin@veeva.com", "123456");
        // Inject the credentials
        provider.setCredentials(AuthScope.ANY, credentials);
        // Set the default credentials provider
        httpClientBuilder.setDefaultCredentialsProvider(provider);
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

        String result = "";
        HttpGet httpGet = null;
        HttpResponse httpResponse = null;
        HttpEntity entity = null;
        httpGet = new HttpGet(URI);
        try {
            httpResponse = closeableHttpClient.execute(httpGet);
            entity = httpResponse.getEntity();
            if( entity != null ){
                result = EntityUtils.toString(entity);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭连接
        try {
            closeableHttpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSON.parseObject(result);
        boolean data = jsonObject.containsKey("data");
        boolean errors = jsonObject.containsKey("errors");
        System.out.println(data);
        System.out.println(errors);
        //
        System.out.println(result);
    }
    @Test
    public void send(){
        String url = "https://uathmp.veevasfa.cn/public-api/v1/objects/account/records?filters[record_type]=hospital&limit=200&sort=-modified_on";
        try {
            // 创建HttpClientBuilder
            HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
            HttpGet get = new HttpGet(url);
            //添加http头信息
            get.addHeader("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("admin@veeva.com" + ":" + "123456").getBytes()));
            String result = "";
            HttpResponse httpResponse = null;
            HttpEntity entity = null;
            try {
                httpResponse = closeableHttpClient.execute(get);
                entity = httpResponse.getEntity();
                if( entity != null ){
                    result = EntityUtils.toString(entity);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            // 关闭连接
            closeableHttpClient.close();

            System.out.println(result);
        }catch (Exception e) {
            System.out.println(e.getStackTrace());
        }
    }
    @Test
    public void testFormat(){
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.00");
//        // 获取当前时间减去24小时之后的时间
//        String dateTime = format.format(DateUtils.addDay(new Date(),-24));
//        System.out.println(dateTime);
        int maxLength = 6;
        String year = "2020";
        int maxNo = 1111;
        String no = year + "-" + String.format("%0"+maxLength+"d",maxNo);
        System.out.println(no);
    }
}
